Public Class Popup_Contact_Inframe
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents dgDataList As System.Web.UI.WebControls.DataGrid
    Protected WithEvents btnRefresh As System.Web.UI.WebControls.Button
    Protected WithEvents removeCID As System.Web.UI.HtmlControls.HtmlInputHidden

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Me.IsPostBack Then
            Dim cId As Integer
            cId = CType(Session("CreateOrder_SelectedCustomerID"), Integer)

            BindCustomerContactDataGrid(CCustomerContact.GetCustomerContactList(cId))
        End If
    End Sub

    Private Property SortExpression() As String
        Get
            If (ViewState("SortExpression") Is Nothing) Then
                Return "NAME"
            Else
                Return CStr(ViewState("SortExpression"))
            End If
        End Get
        Set(ByVal Value As String)
            ViewState("SortExpression") = Value
        End Set
    End Property

    Private Property SortDirection() As String
        Get
            If ViewState("SortDirection") Is Nothing Then
                Return " ASC"
            Else
                Return CStr(ViewState("SortDirection"))
            End If
        End Get
        Set(ByVal Value As String)
            ViewState("SortDirection") = Value
        End Set
    End Property

    Private Sub BindCustomerContactDataGrid(ByVal dt As DataTable)

        Session("Popup_CustomerContactRefList") = dt
        Me.dgDataList.DataSource = dt
        Me.dgDataList.DataBind()
    End Sub

    Private Sub dgDataList_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgDataList.SortCommand
        ' Retrieve the data source from session state.
        Dim dt As DataTable = CType(Session("Popup_CustomerContactRefList"), DataTable)

        ' Create a DataView from the DataTable.
        Dim dv As DataView = New DataView(dt)

        ' The DataView provides an easy way to sort. Simply set the
        ' Sort property with the name of the field to sort by.
        If e.SortExpression <> Me.SortExpression Then
            dv.Sort = e.SortExpression & " ASC"
            Me.SortExpression = e.SortExpression
            Me.SortDirection = " DESC"
        Else
            dv.Sort = e.SortExpression & Me.SortDirection
            If (Me.SortDirection = " DESC") Then
                Me.SortDirection = " ASC"
            Else
                Me.SortDirection = " DESC"
            End If
        End If


        ' Rebind the data source and specify that it should be sorted
        ' by the field specified in the SortExpression property.
        dgDataList.DataSource = dv
        dgDataList.DataBind()

    End Sub

    Private Sub btnRefresh_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        Dim cId As Integer
        cId = CType(Session("CreateOrder_SelectedCustomerID"), Integer)

        BindCustomerContactDataGrid(CCustomerContact.GetCustomerContactList(cId))
    End Sub
    Protected Sub RemoveCustomerContact_Command(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim cId As System.Int32
        cId = CInt(removeCID.Value)
        Dim obj As New CCustomerContact
        obj.Load(cId)
        obj.Remove()
        btnRefresh_Click(Me, EventArgs.Empty)
    End Sub

    Protected Function GetCreateNewLink() As String
        Dim cId As Integer
        cId = CType(Session("CreateOrder_SelectedCustomerID"), Integer)

        Return "CustomerContactDialog.aspx?cid=" + cId.ToString()
    End Function

    Protected Function GetSelectedCustomer() As Integer
        Dim cId As Integer
        cId = CType(Session("CreateOrder_SelectedCustomerID"), Integer)

        Return cId
    End Function
End Class
